package com.huawei.java.main;


import com.huawei.java.main.deployentity.DeployServer;
import com.huawei.java.main.deployentity.DeployVM;
import com.huawei.java.main.inputentity.Require;

import java.util.*;

/**
 * 输出结果
 */
public class Output {
    /**
     *
     * @param currentDay 当前是第几天
     * @param startIndex 当天服务器的开始编号
     */
    public Output(int currentDay,int startIndex) {
        this.dailyAddRequireList =Input.dailyRequireList.get(currentDay).get("addList");
        this.currentDay=currentDay;
        this.startIndex=startIndex;
    }
    private int currentDay;
    private int startIndex;
    private List<Require> dailyAddRequireList;//当天对应的添加请求
    private final List<DeployVM> dailyDeployVM = new ArrayList<>();//一天部署的虚拟机信息
    private final List<DeployServer> dailyDeployServer = new ArrayList<>();//一天购买的服务器信息

    public List<DeployVM> getDailyDeployVM() {
        return dailyDeployVM;
    }

    public void addDailyDeployVM(DeployVM deployVM) {
        this.dailyDeployVM.add(deployVM);
    }

    public List<DeployServer> getDailyDeployServer() {
        return dailyDeployServer;
    }

    public void addDailyDeployServer(DeployServer deployServer) {
        this.dailyDeployServer.add(deployServer);
    }

    public void result(){

        dailyDeployServer.sort((o1, o2) -> o1.getServerType().compareTo(o2.getServerType()));//按照服务器类型排序
        Map<String,Integer> serverMap = new LinkedHashMap<>();
        for (DeployServer deployServer:dailyDeployServer){
            deployServer.setId(startIndex++);
            serverMap.put(deployServer.getServerType(),findServerTypeNum(deployServer.getServerType()));
        }


        //1.统计购买了几种服务器
//        Map<String,Integer> serverMap = new HashMap<>();
//        for (DeployServer deployServer:dailyDeployServer){
//            serverMap.put(deployServer.getServerType(), findServerTypeNum(deployServer.getServerType()));
//        }

        System.out.printf("(purchase, %d)%n", serverMap.size());//买了几种类型的服务器

        //2.具体购买的服务器型号
        for (Map.Entry<String,Integer> obj: serverMap.entrySet()){

            System.out.printf("(%s, %d)%n", obj.getKey(), obj.getValue());
        }
        // TODO: 2021/3/16 记得考虑迁移如何实现
        //3.迁移。暂时不考虑迁移的事情
        System.out.println("(migration, 0)");

        //4.虚拟机部署。注意顺序！！！每天的请求和如何部署要一一对应，顺序不能乱
        for (Require require:dailyAddRequireList){
            DeployVM deployVM=findDeployNMByRequire(require);
            String node= deployVM.getNode();
            if (node.equals("AB"))
                System.out.printf("(%d)%n", deployVM.getDeployServer().getId());
            else
                System.out.printf("(%d, %s)%n", deployVM.getDeployServer().getId(), node);
        }
    }

    /**
     * 找到该类型的服务器类型购买了多少台
     * @param serverType
     * @return
     */
    int findServerTypeNum(String serverType){
        int sum=0;
        for (DeployServer deployServer:dailyDeployServer){
            if (serverType.equals(deployServer.getServerType()))
                sum++;
        }
        return sum;
    }

    /**
     * 通过请求寻找当天对应的部署虚拟机信息
     * 只需要在当天部署的服务器中寻找
     * @param require
     * @return
     */
    DeployVM findDeployNMByRequire(Require require){
        long vmId = require.getVMId();
        for (DeployVM deployVM:dailyDeployVM){
            if (deployVM.getId()==vmId) return deployVM;
        }
        return null;
    }
}
